using Autofac;
using CoreManager.Repository.Repositories;
using Microsoft.Extensions.Configuration;
namespace CoreManager.Core.Configuration
{
    /// <summary>
    /// Autofac依赖注入配置模块
    /// </summary>
    public class AutofacModule : Module
    {
        private readonly IConfiguration _configuration;

        public AutofacModule(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        protected override void Load(ContainerBuilder builder)
        {
            // 注册通用仓储
            builder.RegisterGeneric(typeof(Repository<>))
                   .As(typeof(IRepository<>))
                   .InstancePerLifetimeScope();

            // 注册服务将在API层进行，避免循环依赖
        }
    }

    /// <summary>
    /// 条件注册模块 - 根据配置条件注册服务
    /// </summary>
    public class ConditionalModule : Module
    {
        private readonly IConfiguration _configuration;

        public ConditionalModule(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        protected override void Load(ContainerBuilder builder)
        {
            // 根据配置条件注册Redis缓存
            var redisConnectionString = _configuration.GetConnectionString("Redis");
            if (!string.IsNullOrEmpty(redisConnectionString))
            {
                // 注册Redis相关服务
                // builder.RegisterType<RedisCacheService>().As<ICacheService>().SingleInstance();
            }
        }
    }

    /// <summary>
    /// 环境特定模块 - 根据环境注册不同的服务实现
    /// </summary>
    public class EnvironmentModule : Module
    {
        private readonly string _environmentName;

        public EnvironmentModule(string environmentName)
        {
            _environmentName = environmentName;
        }

        protected override void Load(ContainerBuilder builder)
        {
            if (_environmentName == "Development")
            {
                // 开发环境特定注册
            }
            else if (_environmentName == "Production")
            {
                // 生产环境特定注册
            }
        }
    }
}